package com.en.sharing.tostring;

import com.alibaba.fastjson.JSON;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 测试 toString 和 JSON.toJSONString 的耗时
 *
 * @author 付晓飞
 * @date 2024/3/11 8:11 下午
 */
public class ToStringTest {

    public static void main(String[] args) {
        final Person person = buildPerson();
        final long begine = System.nanoTime();
        for (int i = 0; i < 500000; i++) {
            JSON.toJSONString(person);
        }
        final long jsonEnd = System.nanoTime();
        // json耗时：594487243
        System.out.println("json耗时：" + (jsonEnd - begine));

        for (int i = 0; i < 500000; i++) {
            person.toString();
        }
        final long toStringEnd = System.nanoTime();
        // toString耗时：265938181
        System.out.println("toString耗时：" + (toStringEnd - jsonEnd));
    }

    private static Person buildPerson() {
        Person person = new Person();
        person.setId(1);
        person.setName("张三");
        person.setAge(18);
        person.setSex("男");
        person.setAddress("北京");
        person.setCreateTime(new Date());
        person.setUpdateTime(new Date());
        person.setIncome(new BigDecimal(15000.00D));
        return person;
    }

}
